<template>
  <div class="login-container">
    <div class="nav">
      <ul class="loglist">
        <li><img src="../../assets/logo.png" alt="" class="log"></li>
        <li>变革</li>
        <li>创新</li>
        <li>当责</li>
        <li>卓越</li>
      </ul>
    </div>
    <div class="content">
      <img src="../../assets/bg.jpg" alt="" title="tcl" class="tclBg">
      <div class="formContent">
        <el-form ref="formRef" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on"
          label-position="left">
          <div class="logo-mimin">
            <img src="/static/logo-mimi.png" alt="">
          </div>
          <div class="title-container">
            <h3 class="title">成都工厂综合管理平台</h3>
          </div>

          <el-form-item prop="username">
            <span class="svg-container">
              <svg-icon name="ele-UserFilled" />
            </span>
            <el-input ref="username" v-model="loginForm.username" placeholder="Username" name="username" type="text"
              tabindex="1" auto-complete="on" />
          </el-form-item>

          <el-form-item prop="password">
            <span class="svg-container">
              <svg-icon name="ele-Lock" />
            </span>
            <el-input :key="passwordType" ref="passwordRef" v-model="loginForm.password" :type="passwordType"
              placeholder="Password" name="password" tabindex="2" auto-complete="on" @keyup.enter="handleLogin" />
            <span class="show-pwd" @click="showPwd">
              <svg-icon :name="passwordType === 'password' ? 'ele-Hide' : 'ele-View'" />
            </span>
          </el-form-item>

          <el-button :loading="loading" type="primary" style="width: 100%; height: 40px" @click.prevent="handleLogin">登
            录</el-button>
          <div class="forgetPwd">
            <span>忘记密码？</span>
          </div>
          <div class="otherLogin">
            <div class="character">第三方快捷登录</div>
            <div class="wxicon">
              <img src="../../assets/wx.png" alt="weixin">
            </div>
          </div>
        </el-form>
      </div>

    </div>
    <div class="footer">©TCL王牌电器（成都）有限公司
    </div>
  </div>
</template>

<script lang="ts">
export default {
  name: 'LoginView'
}
</script>

<script lang="ts" setup>
import { useUserInfoStore } from '@/stores/userInfo'
import { ElMessage, type FormInstance } from 'element-plus'
import { nextTick, ref, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'

const userInfoStore = useUserInfoStore()
const route = useRoute()
const router = useRouter()
const loginForm = ref({
  username: 'admin',
  password: '111111'
})
const loading = ref(false)
const passwordType = ref('password')
const redirect = ref('')
const passwordRef = ref<HTMLInputElement>()
const formRef = ref<FormInstance>()

const validateUsername = (rule: any, value: any, callback: any) => {
  if (value.length < 4) {
    callback(new Error('用户名长度不能小于4位'))
  } else {
    callback()
  }
}
const validatePassword = (rule: any, value: any, callback: any) => {
  if (value.length < 6) {
    callback(new Error('密码长度不能小于6位'))
  } else {
    callback()
  }
}

const loginRules = {
  username: [{ required: true, validator: validateUsername }],
  password: [{ required: true, trigger: 'blur', validator: validatePassword }]
}

watch(
  route,
  () => {
    redirect.value = route.query && (route.query.redirect as string)
  },
  { immediate: true }
)

/* 
切换密码的显示/隐藏
*/
const showPwd = () => {
  if (passwordType.value === 'password') {
    passwordType.value = 'text'
  } else {
    passwordType.value = 'password'
  }
  nextTick(() => {
    passwordRef.value?.focus()
  })
}

/* 
点击登录的回调
*/
const handleLogin = async () => {
  await formRef.value?.validate()
  loading.value = true
  const { username, password } = loginForm.value
  try {
    await userInfoStore.login(username, password)
    router.push({ path: redirect.value || '/' })
  } catch (error) {
    // 登录失败
    // 先关闭之前的提醒
    ElMessage.closeAll()
    ElMessage({
      message: '密码或用户名错误',
      type: 'error'
    })
  }
  finally {
    loading.value = false
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #000;
$cursor: #080808;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  background-image: url(../../assets/bg_1.png);
  background-size: cover;

  .el-input {
    display: inline-block;
    height: 40px;
    width: 85%;

    .el-input__wrapper {
      width: 100%;
      background-color: transparent;
      box-shadow: none;

      input {
        background: transparent;
        border: 0px;
        appearance: none;
        border-radius: 0px;
        padding: 12px 5px 12px 15px;
        color: $light_gray;
        height: 47px;
        caret-color: $cursor;

        &:-webkit-autofill {
          box-shadow: 0 0 0px 1000px $bg inset !important;
          -webkit-text-fill-color: $cursor !important;
        }
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #070707;
$zi_color: #666;

.login-container {
  height: 100%;
  width: 100%;
  // background-color: $bg;s
  overflow: hidden;

  // 头部区域
  .nav {
    margin-left: 6%;

    .loglist {
      list-style: none;
      display: flex;
      align-items: center;
      margin: 30px 0;

      .log {
        width: 100px;
      }

      li {
        padding: 0px 10px;
        border-right: 3px solid $zi_color;
        font-size: 18px;
        font-weight: 900;
        color: $zi_color;

        &:last-child {
          border: none;

        }
      }

      li:first-child {
        border: none;
        /* 移除第一项的上边框 */
      }
    }
  }


  // 中间区域
  .content {
    display: flex;
    width: calc(100%);
    height: calc(80%);

    .tclBg {
      width: calc(61%);
      height: calc(80%);
      margin-right: calc(2%);
      margin-left: calc(6%);
    }

    /* 背景图等比缩放以适应容器 */
    .formContent {
      text-align: center;
      width: calc(25%);
      height: calc(80%);
      background-color: rgba(184, 182, 182, 0.461);
      display: flex;
      align-items: center;
      justify-content: center;
      border-radius: 5%;

      .forgetPwd {
        width: 100%;
        // height: 20px;
        overflow: hidden;
        margin-bottom: 10px;
        margin-top: 5px;

        span {
          color: rgb(34, 0, 255);
          float: right;
        }

      }

      .otherLogin {
        position: relative;

        .character {
          font-size: 12px;
        }

        .character::before {
          content: '';
          border-bottom: 1px solid #606060;
          width: 113px;
          position: absolute;
          top: 7px;
          left: 0;
        }

        .character::after {
          content: '';
          border-bottom: 1px solid #606060;
          width: 113px;
          position: absolute;
          top: 7px;
          right: 0;
        }

        .wxicon {
          padding: 5px;



          img {
            width: 45px;
            height: 45px;
            border-radius: 50%;

            &:hover {
              background-color: greenyellow;
            }
          }
        }

      }
    }
  }

  .login-form {
    width: 350px;
    max-width: 100%;

  }

  .tips {
    font-size: 14px;
    // color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    margin: 20px 0 20px 0;
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;

    .title {
      font-size: 20px;
      color: $light_gray;
      text-align: center;
      font-weight: bold;
      color: $zi_color;

    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 10px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

  // 尾巴.
  .footer {
    color: $zi_color;
    text-align: center;
  }
}
</style>
